package com.zhonghui.wms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.common.utils.StringUtils;
import com.zhonghui.wms.domain.WmsInventoryDetails;
import com.zhonghui.wms.domain.WmsMaterialInventory;
import com.zhonghui.wms.domain.vo.WmsInventoryDetailsVo;
import com.zhonghui.wms.domain.vo.WmsMaterialInventoryVo;
import com.zhonghui.wms.domain.vo.WmsRelocationInformationVo;
import com.zhonghui.wms.mapper.*;
import com.zhonghui.wms.service.IWmsMaterialInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhonghui.wms.domain.WmsRelocationInformation;
import com.zhonghui.wms.service.IWmsRelocationInformationService;

/**
 * 智能仓储WMS-移库信息Service业务层处理
 *
 * @author zhonghui
 * @date 2023-08-02
 */
@Service
public class WmsRelocationInformationServiceImpl implements IWmsRelocationInformationService
{
    @Autowired
    private WmsRelocationInformationMapper wmsRelocationInformationMapper;

    @Autowired
    private WmsInventoryDetailsMapper wmsInventoryDetailsMapper;

    @Autowired
    private IWmsMaterialInventoryService wmsMaterialInventoryService;

    /**
     * 查询智能仓储WMS-移库信息
     *
     * @param id 智能仓储WMS-移库信息主键
     * @return 智能仓储WMS-移库信息
     */
    @Override
    public WmsRelocationInformation selectWmsRelocationInformationById(Long id)
    {
        return wmsRelocationInformationMapper.selectWmsRelocationInformationById(id);
    }

    /**
     * 查询智能仓储WMS-移库信息列表
     *
     * @param wmsRelocationInformation 智能仓储WMS-移库信息
     * @return 智能仓储WMS-移库信息
     */
    @Override
    public List<WmsRelocationInformationVo> selectWmsRelocationInformationList(WmsRelocationInformation wmsRelocationInformation)
    {
        return wmsRelocationInformationMapper.selectWmsRelocationInformationList(wmsRelocationInformation);
    }

    /**
     * 新增智能仓储WMS-移库信息
     *
     * @param wmsRelocationInformation 智能仓储WMS-移库信息
     * @return 结果
     */
    @Override
    public int insertWmsRelocationInformation(WmsRelocationInformation wmsRelocationInformation)
    {
        return wmsRelocationInformationMapper.insertWmsRelocationInformation(wmsRelocationInformation);
    }

    /**
     * 修改智能仓储WMS-移库信息
     *
     * @param wmsRelocationInformation 智能仓储WMS-移库信息
     * @return 结果
     */
    @Override
    public int updateWmsRelocationInformation(WmsRelocationInformation wmsRelocationInformation)
    {
        return wmsRelocationInformationMapper.updateWmsRelocationInformation(wmsRelocationInformation);
    }

    /**
     * 批量删除智能仓储WMS-移库信息
     *
     * @param ids 需要删除的智能仓储WMS-移库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsRelocationInformationByIds(Long[] ids)
    {
        return wmsRelocationInformationMapper.deleteWmsRelocationInformationByIds(ids);
    }

    /**
     * 删除智能仓储WMS-移库信息信息
     *
     * @param id 智能仓储WMS-移库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsRelocationInformationById(Long id)
    {
        return wmsRelocationInformationMapper.deleteWmsRelocationInformationById(id);
    }

    @Override
    public void batchWmsRelocationInformation(List<WmsRelocationInformation> wmsRelocationInformationList) {
        List<WmsRelocationInformation> resultList = new ArrayList<WmsRelocationInformation>();
        wmsRelocationInformationList.stream().forEach(item->{
            // 构建查询源仓库库存明细条件
            WmsInventoryDetailsVo sourceVo = new WmsInventoryDetailsVo();
            sourceVo.setMaterialId(item.getMaterialId());
            sourceVo.setWarehouseId(item.getSourceWarehouse());
            sourceVo.setWarehouseAreaId(item.getSourceArea());
            sourceVo.setWarehouseSeatId(item.getSourceSeat());
            sourceVo.setBatchNumber(item.getBatchNumber());
            List<WmsInventoryDetailsVo> sourceVoList = wmsInventoryDetailsMapper.selectWmsInventoryDetailsList(sourceVo);
            // 获取明细信息
            WmsInventoryDetailsVo source = sourceVoList.get(0);
            // 当前源仓库库存量
            double existingTotal = source.getExistingInventory() == null ? 0 : source.getExistingInventory();
            // 移库数量
            double relocationQuantity = item.getRelocationQuantity() == null ? 0 : item.getRelocationQuantity();
            // 移库后的源仓库数量
            double total = existingTotal - relocationQuantity;
            source.setExistingInventory(total);
            // 调用修改明细的方法更改数量
            wmsInventoryDetailsMapper.updateWmsInventoryDetails(source);
            // 更新源仓库总库存
            wmsMaterialInventoryService.updateMaterialTotalInventory(source.getInventoryId());
            // 构建目标仓库查询条件
            WmsMaterialInventoryVo targetVo = new WmsMaterialInventoryVo();
            targetVo.setWarehouseId(item.getTargetWarehouse());
            targetVo.setMaterialId(item.getMaterialId());
            //检查目标仓库是否有此物料
            List<WmsMaterialInventoryVo> targetVoList = wmsMaterialInventoryService.selectWmsMaterialInventoryList(targetVo);
            if(targetVoList != null && !targetVoList.isEmpty()) {
                WmsMaterialInventoryVo targetInventoryVo = targetVoList.get(0);
                //有此物料直接更新明细库存
                WmsInventoryDetailsVo targetDetailsVo = new WmsInventoryDetailsVo();
                targetDetailsVo.setMaterialId(item.getMaterialId());
                targetDetailsVo.setWarehouseId(item.getTargetWarehouse());
                targetDetailsVo.setWarehouseAreaId(item.getTargetArea());
                targetDetailsVo.setWarehouseSeatId(item.getTargetSeat());
                targetDetailsVo.setBatchNumber(item.getBatchNumber());
                // 根据条件查询需要更新的库存明细
                List<WmsInventoryDetailsVo> targetInventoryList = wmsInventoryDetailsMapper.selectWmsInventoryDetailsList(targetDetailsVo);
                if(targetInventoryList != null && !targetInventoryList.isEmpty()) {
                    // 库存明细中包含了目标仓库物料即进行更新
                    WmsInventoryDetailsVo targetInventoryDetailsVo = targetInventoryList.get(0);
                    double inventory = targetInventoryDetailsVo.getExistingInventory() == null ? 0:targetInventoryDetailsVo.getExistingInventory();
                    targetInventoryDetailsVo.setExistingInventory(inventory + relocationQuantity);
                    wmsInventoryDetailsMapper.updateWmsInventoryDetails(targetInventoryDetailsVo);
                }else {
                    // 库存明细中不包含目标仓库物料即进行新增明细
                    WmsInventoryDetailsVo addInventoryDetailsVo = new WmsInventoryDetailsVo();
                    addInventoryDetailsVo.setInventoryId(targetInventoryVo.getId());
                    addInventoryDetailsVo.setWarehouseId(item.getTargetWarehouse());
                    addInventoryDetailsVo.setWarehouseAreaId(item.getTargetArea());
                    addInventoryDetailsVo.setWarehouseSeatId(item.getTargetSeat());
                    addInventoryDetailsVo.setBatchNumber(item.getBatchNumber());
                    addInventoryDetailsVo.setExistingInventory(relocationQuantity);
                    addInventoryDetailsVo.setManufactureDate(source.getManufactureDate());
                    addInventoryDetailsVo.setManufacturer(source.getManufacturer());
                    wmsInventoryDetailsMapper.insertWmsInventoryDetails(addInventoryDetailsVo);
                }
                // 更新目标仓库总库存
                wmsMaterialInventoryService.updateMaterialTotalInventory(targetInventoryVo.getId());
            }else {
                // 目标仓库不存在此物料即新增
                WmsMaterialInventoryVo wmsMaterialInventoryVo = new WmsMaterialInventoryVo();
                wmsMaterialInventoryVo.setMaterialId(item.getMaterialId());
                wmsMaterialInventoryVo.setWarehouseId(item.getTargetWarehouse());
                List<WmsInventoryDetails> wmsInventoryDetailsList = new ArrayList<WmsInventoryDetails>();
                WmsInventoryDetails wmsInventoryDetails = new WmsInventoryDetails();
                wmsInventoryDetails.setWarehouseAreaId(item.getTargetArea());
                wmsInventoryDetails.setWarehouseSeatId(item.getTargetSeat());
                wmsInventoryDetails.setBatchNumber(item.getBatchNumber());
                wmsInventoryDetails.setExistingInventory(relocationQuantity);
                wmsInventoryDetails.setMaterialId(item.getMaterialId());
                wmsInventoryDetails.setManufactureDate(source.getManufactureDate());
                wmsInventoryDetails.setManufacturer(source.getManufacturer());
                wmsInventoryDetailsList.add(wmsInventoryDetails);
                wmsMaterialInventoryVo.setWmsInventoryDetailsList(wmsInventoryDetailsList);
                wmsMaterialInventoryService.insertWmsMaterialInventory(wmsMaterialInventoryVo);
                // 更新目标仓库总库存
                wmsMaterialInventoryService.updateMaterialTotalInventory(wmsMaterialInventoryVo.getId());
            }
            resultList.add(item);
        });
        // 判断筛选后的移库信息是否为空
        if (resultList != null && !resultList.isEmpty()){
            wmsRelocationInformationMapper.batchWmsRelocationInformation(resultList);
        }
    }
}
